/**
 * 主题工具函数
 * 根据themeColor值动态获取对应的图片目录路径
 */

/**
 * 根据主题色获取图片目录路径
 * @param {string} themeColor - 主题色值
 * @returns {string} 图片目录路径
 */
export function getThemeImagePath(themeColor) {
  const themeMap = {
    '0': '/static/images/default',
    '1': '/static/images/green',
    '2': '/static/images/orange',
    '3': '/static/images/black',
    '4': '/static/images/purple'
  }
  
  // 默认返回主题0的路径
  return themeMap[themeColor] || themeMap['0']
}

/**
 * 根据主题色获取TabBar图标路径
 * @param {string} themeColor - 主题色值
 * @param {string} iconName - 图标名称
 * @param {boolean} isSelected - 是否为选中状态
 * @returns {string} 完整的图标路径
 */
export function getTabBarIconPath(themeColor, iconName, isSelected = false) {
  const basePath = getThemeImagePath(themeColor)
  const suffix = isSelected ? '_' : ''
  return `${basePath}/tabbar/${iconName}${suffix}.png`
}

/**
 * 根据主题色获取首页导航图标路径
 * @param {string} themeColor - 主题色值
 * @param {string} iconName - 图标名称
 * @returns {string} 完整的图标路径
 */
export function getIndexIconPath(themeColor, iconName) {
  const basePath = getThemeImagePath(themeColor)
  return `${basePath}/index/${iconName}.png`
}

/**
 * 根据主题色获取我的页面图标路径
 * @param {string} themeColor - 主题色值
 * @param {string} iconName - 图标名称
 * @returns {string} 完整的图标路径
 */
export function getMineIconPath(themeColor, iconName) {
  const basePath = getThemeImagePath(themeColor)
  return `${basePath}/mine/${iconName}.png`
}

/**
 * 根据主题色获取对应的颜色值
 * @param {string} themeColor - 主题色值
 * @returns {object} 包含各种颜色的对象
 */
export function getThemeColors(themeColor) {
  const colorMap = {
    '0': {
      primary: '#409EFF',
      primaryLight: '#66B1FF',
      primaryDark: '#337ECC',
      background: '#F5F7FA',
      text: '#303133',
      textSecondary: '#606266',
      border: '#DCDFE6',
      success: '#67C23A',
      warning: '#E6A23C',
      danger: '#F56C6C'
    },
    '1': {
      primary: '#1B5E20',
      primaryLight: '#2E7D32',
      primaryDark: '#0D4F12',
      background: '#F1F8E9',
      text: '#1B5E20',
      textSecondary: '#388E3C',
      border: '#C8E6C9',
      success: '#1B5E20',
      warning: '#F57C00',
      danger: '#D32F2F'
    },
    '2': {
      primary: '#FA8C16',
      primaryLight: '#FFA940',
      primaryDark: '#D46B08',
      background: '#FFF7E6',
      text: '#262626',
      textSecondary: '#595959',
      border: '#FFE7BA',
      success: '#52C41A',
      warning: '#FA8C16',
      danger: '#FF4D4F'
    },
    '3': {
      primary: '#262626',
      primaryLight: '#434343',
      primaryDark: '#1F1F1F',
      background: '#F5F5F5',
      text: '#000000',
      textSecondary: '#262626',
      border: '#D9D9D9',
      success: '#52C41A',
      warning: '#FAAD14',
      danger: '#FF4D4F'
    },
    '4': {
      primary: '#722ED1',
      primaryLight: '#9254DE',
      primaryDark: '#531DAB',
      background: '#F9F0FF',
      text: '#262626',
      textSecondary: '#595959',
      border: '#EFDBFF',
      success: '#52C41A',
      warning: '#FAAD14',
      danger: '#FF4D4F'
    }
  }
  
  return colorMap[themeColor] || colorMap['0']
}

/**
 * 获取主题色的主要颜色
 * @param {string} themeColor - 主题色值
 * @returns {string} 主要颜色值
 */
export function getThemePrimaryColor(themeColor) {
  const colors = getThemeColors(themeColor)
  return colors.primary
}

/**
 * 从全局配置中获取主题色
 * @returns {string} 主题色值，默认为'1'
 */
export async function getThemeColorFromGlobal() {
  try {
    const app = getApp()
    // 使用实时获取配置方法
    const programConfig = await app.getProgramConfig()
    // 支持theme_color和themeColor两种字段名
    return programConfig?.theme_color || programConfig?.themeColor || '1'
  } catch (error) {
    console.warn('获取全局主题色失败，使用默认值:', error)
    return '1'
  }
}

/**
 * 设置全局主题色
 * @param {string} themeColor - 主题色值
 */
export function setThemeColorToGlobal(themeColor) {
  try {
    const app = getApp()
    if (!app.globalData.programConfig) {
      app.globalData.programConfig = {}
    }
    app.globalData.programConfig.theme_color = themeColor
    app.globalData.programConfig.themeColor = themeColor
    
    // 同时保存到本地存储
    uni.setStorageSync('themeColor', themeColor)
  } catch (error) {
    console.error('设置全局主题色失败:', error)
  }
}